<template>
	<view class="home">
		<!-- 自定义导航栏 -->
		<navbar :isSearch="true" v-model="value" @input="change"></navbar>
		<view class="home-list">
			<view class="label-box" v-if="isHistory">
				<view class="label-header">
					<text class="label-title">搜索历史</text>
					<text class="label-clear" @click="clear">清空</text>
				</view>
				<view v-if="historyList.length>0" class="label-content">
					<view class="label-content__item" v-for="(item, index) in historyList" :key="index" @click="openHistory(item)">
						{{item.name}}
					</view>
				</view>
				<view v-else class="no-data">
					没有搜索历史
				</view>
			</view>
			<list-scroll class="list-scroll" v-else>
				<uni-load-more v-if="loading" status="loading" iconType="snow"></uni-load-more>
			  <view v-if="searchList.length >0">
			  	<list-card  :item="item" v-for="(item,index) in searchList" :key="item._id" @click="setHistory"></list-card>
			  </view>
			  <view class="no-data" v-if="searchList.length ===0 && !loading">
			  	没有搜索相关信息
			  </view>
			</list-scroll> 
		</view>
	</view>
</template>

<script>
	import {mapState} from 'vuex'
	export default {
		data() {
			return {
              // historyList:[],
			  isHistory:true,
			  searchList:[],
			  value:"",
			  loading:false
			};
		},
		computed:{
			...mapState(['historyList'])
		},
		onLoad(){
			// this.getList()
		},
		methods:{
			clear(){
				if(this.historyList.length >0){
				  this.$store.dispatch('clear_history')
				  uni.showToast({
				  	title:'清空完成'
				  })	
				}else {
					uni.showToast({
						title:"暂无历史记录",
						icon:'none'
					})
				}
				
			},
			change(value) {
				// console.log('value' ,value)
				// this.value = value
				if (!value){
					clearTimeout(this.timer)
					this.mark = false
					this.getSearch(value)
					return
				}
				if(!this.mark){
					this.mark = true;
					this.timer = setTimeout(() => {
						this.mark = false;
						this.getSearch(value)
					},1000)
				}
			},
			test(){
				this.$store.dispatch('set_history',{name:'cnm'})
			},
			getSearch(value) {
				if(!value) {
					this.searchList = []
					this.isHistory = true
					return
				}
				this.isHistory = false
				this.loading = true
				this.$api.get_search({
					value
				}).then(res => {
					let {data} = res
				    this.searchList = data
				}).catch(err=> {
					uni.showToast({
						title:'数据请求失败',
						 icon:'none'
					})
				} ).finally(()=>{
					this.loading = false
				})
			},
			setHistory(data) {
				this.$store.dispatch('set_history',{name:this.value})
			},
			openHistory(item) {
				this.value = item.name
				this.getSearch(this.value)
			}
		}
	}
</script>

<style lang="scss">
	page {
		height: 100%;
		background-color: #f5f5f5;
		display: flex;
	}

	.home {
		display: flex;
		flex-direction: column;
		flex: 1;
		// border: 1px solid red;

		.label-box {
			background-color: #fff;
			margin-bottom: 10px;

			.label-header {
				display: flex;
				justify-content: space-between;
				font-size: 14px;
				color: #666;
				padding: 10px 15px;
				border-bottom: 1px #f5f5f5 solid;

				.label-title {
					color: $mk-base-color;
				}

				.label-clear {
					color: #30b33a;
					font-weight: bold;
				}
			}

			.label-content {
				display: flex;
				flex-wrap: wrap;
				padding: 0 15px 15px 15px;
				.label-content__item{
					padding: 2px 10px;
					margin-top: 12px;
					margin-right: 10px;
					border: 1px solid #666;
					border-radius: 5px;
					font-size: 14px;
					color: #666;
				}
			}
		}
		.no-data{
			height: 200px;
			line-height: 200px;
			width: 100%;
			text-align: center;
			font-size: 12px;
			color: #666;
		}
	}
</style>
